{% extends '@WebProfiler/Profiler/layout.html.twig' %}

{% import _self as logger %}

{% block toolbar %}
    {% if collector.counterrors or collector.countdeprecations %}
        {% set icon %}
            <img width="15" height="28" alt="Logs" src="" />
            {% if collector.counterrors %}
                {% set status_color = "red" %}
            {% else %}
                {% set status_color = "yellow" %}
            {% endif %}
            {% set error_count = collector.counterrors + collector.countdeprecations %}
            <span class="sf-toolbar-status sf-toolbar-status-{{ status_color }}">{{ error_count }}</span>
        {% endset %}
        {% set text %}
            {% if collector.counterrors %}
                <div class="sf-toolbar-info-piece">
                    <b>Exception</b>
                    <span class="sf-toolbar-status sf-toolbar-status-red">{{ collector.counterrors }}</span>
                </div>
            {% endif %}
            {% if collector.countdeprecations %}
                <div class="sf-toolbar-info-piece">
                    <b>Deprecated Calls</b>
                    <span class="sf-toolbar-status sf-toolbar-status-yellow">{{ collector.countdeprecations }}</span>
                </div>
            {% endif %}
        {% endset %}
        {% include '@WebProfiler/Profiler/toolbar_item.html.twig' with { 'link': profiler_url } %}
    {% endif %}
{% endblock %}

{% block menu %}
<span class="label">
    <span class="icon"><img src="" alt="Logger"></span>
    <strong>Logs</strong>
    {% if collector.counterrors or collector.countdeprecations %}
        {% set error_count = collector.counterrors + collector.countdeprecations %}
        <span class="count">
            <span>{{ error_count }}</span>
        </span>
    {% endif %}
</span>
{% endblock %}

{% block panel %}
    <h2>Logs</h2>

    {% set priority = request.query.get('priority', 0) %}

    <table>
        <tr>
            <th>Filter</th>
            <td>
                <form id="priority-form" action="" method="get" style="display: inline">
                    <input type="hidden" name="panel" value="logger">
                    <label for="priority">Priority</label>
                    <select id="priority" name="priority" onchange="document.getElementById('priority-form').submit(); ">
                        {# values < 0 are custom levels #}
                        {% for value, text in { 100: 'DEBUG', 200: 'INFO', 250: 'NOTICE', 300: 'WARNING', 400: 'ERROR', 500: 'CRITICAL', 550: 'ALERT', 600: 'EMERGENCY', '-100': 'DEPRECATION only' } %}
                            <option value="{{ value }}"{{ value == priority ? ' selected' : '' }}>{{ text }}</option>
                        {% endfor %}
                    </select>
                    <noscript>
                        <input type="submit" value="refresh">
                    </noscript>
                </form>
            </td>
        </tr>
    </table>

    {% if collector.logs %}
        <ul class="alt">
            {% for log in collector.logs if priority >= 0 and log.priority >= priority or priority < 0 and log.context.type|default(0) == priority %}
                <li class="{{ cycle(['odd', 'even'], loop.index) }}{% if log.priority >= 400 %} error{% elseif log.priority >= 300 %} warning{% endif %}">
                    {{ logger.display_message(loop.index, log) }}
                </li>
            {% else %}
                <li><em>No logs available for this priority.</em></li>
            {% endfor %}
        </ul>
    {% else %}
        <p>
            <em>No logs available.</em>
        </p>
    {% endif %}
{% endblock %}


{% macro display_message(log_index, log) %}
    {% if constant('Symfony\\Component\\Debug\\ErrorHandler::TYPE_DEPRECATION') == log.context.type|default(0) %}
        DEPRECATION -  {{ log.message }}
        {% set id = 'sf-call-stack-' ~ log_index %}
        <a href="#" onclick="Sfjs.toggle('{{ id }}', document.getElementById('{{ id }}-on'), document.getElementById('{{ id }}-off')); return false;">
            <img class="toggle" id="{{ id }}-off" alt="-" src="" style="display:none">
            <img class="toggle" id="{{ id }}-on" alt="+" src="" style="display:inline">
        </a>
        {% for index, call in log.context.stack if index > 1 %}
            {% if index == 2 %}
                <ul class="sf-call-stack" id="{{ id }}" style="display: none">
            {% endif %}
            {% if call.class is defined %}
                {% set from = call.class|abbr_class ~ '::' ~ call.function|abbr_method() %}
            {% elseif call.function is defined %}
                {% set from = call.function|abbr_method %}
            {% elseif call.file is defined %}
                {% set from = call.file %}
            {% else %}
                {% set from = '-' %}
            {% endif %}

            <li>Called from {{ call.file is defined and call.line is defined ? call.file|format_file(call.line, from) : from|raw }}</li>

            {% if index == log.context.stack|length - 1 %}
                </ul>
            {% endif %}
        {% endfor %}
    {% else %}
        {{ log.priorityName }} - {{ log.message }}
        {% if log.context is defined and log.context is not empty %}
            <br />
            <small>
                <strong>Context</strong>: {{ log.context|json_encode(64 b-or 256) }}
            </small>
        {% endif %}
    {% endif %}
{% endmacro %}
